<?php
/*
 CI Library Proxy Server Detection
 Written by: Ciprian Dimofte - 01-01-2010
 */
class Proxy_detector {

	/**
	* CONSTRUCTOR
	*	Set defaults...
	*/
	function __construct(){
	    $this->CI =& get_instance();
	    
		$this->config = array();
		$this->lastLog = "";

        //set default headers
		$this->scan_headers = array(
			'HTTP_VIA',
			'HTTP_X_FORWARDED_FOR',
			'HTTP_FORWARDED_FOR',
			'HTTP_X_FORWARDED',
			'HTTP_FORWARDED',
			'HTTP_CLIENT_IP',
			'HTTP_FORWARDED_FOR_IP',
			'VIA',
			'X_FORWARDED_FOR',
			'FORWARDED_FOR',
			'X_FORWARDED',
			'FORWARDED',
			'CLIENT_IP',
			'FORWARDED_FOR_IP',
			'HTTP_PROXY_CONNECTION'
		);
	}


	/**
	* VOID setHeader( STRING $trigger )
	*	Set new header trigger...
	*/
	function setHeader($trigger){
		$this->scan_headers[] = $trigger;
	}


	/**
	* ARRAY $triggers = getHeaders( VOID )
	*	Get all triggers in one array
	*/
	function getHeaders(){
    	return $this->scan_headers;
	}


	/**
	* VOID setConfig( STRING $key,  STRING $value)
	*	Set config line...
	*/
	function setConfig($key,$value){
		$this->config[$key] = $value;
	}


	/**
	* MIXED $config = getConfig( [STRING $key] )
	*	Get all config in one array, or only one config value as a string.
	*/
	function getConfig($key=''){
    	if($key)
    		return $this->config[$key];
    	else
    		return $this->config;
	}


	/**
	* STRING $log = getLog( VOID )
	*	Get last logged information. Only works AFTER calling detect()!
	*/
	function getLog(){
		return $this->lastLog;
	}


	/**
	* BOOL $proxy = detect( VOID )
	*	Start detection and return true if a proxy server is detected...
	*/
	function detect(){
		$log = "";

		//scan all headers
		foreach($this->scan_headers as $i){
			//proxy detected? lets log...
			if($this->CI->input->server($i , TRUE))
				$log.= "trigger $i: ".$this->CI->input->server($i, TRUE)."\n";
		}

    	//let's do something...
		if($log){
			$log = $this->lastLog = date("Y-m-d H:i:s")."\nDetected proxy server: ".gethostbyaddr($this->CI->input->server('REMOTE_ADDR', TRUE))." ({$this->CI->input->server('REMOTE_ADDR' , TRUE)})\n".$log;

			//mail message
            if($this->getConfig('MAIL_ALERT_TO'))
				mail($this->getConfig('MAIL_ALERT_TO'),"Proxy detected at {$this->CI->input->server('REQUEST_URI' , TRUE)}",$log);

			//write to file
			$f = $this->getConfig('LOG_FILE');
            if($f){
				if(is_writable($f)){
					$fp = fopen($f,'a');
					fwrite($fp,"$log\n");
					fclose($fp);
            	}else{
					die("<strong>Fatal Error:</strong> Proxy Detector couldn't write to log file: '<strong>$f</strong>'<br>Please check if the path exists and is writable for the webserver or php...");
            	}
            }

			//done
			return true;
		}

		//nope, no proxy was logged...
		return false;
	}
}
?>